package com.example.javaee.controller;

import com.example.javaee.service.ImageService;
import com.example.javaee.service.ImageService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@CrossOrigin
public class ImageController {

    @Autowired
    ImageService imageService;

    @PostMapping("/image/upload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file,
                         @RequestParam("id") Integer userId){
        return imageService.upload(userId,file);
    }

    @GetMapping("/image/{userId}")
    public String getHeaderStringByUserId(@PathVariable Integer userId) throws IOException
    {
        return imageService.getHeaderString(userId);
    }



    /*
    @GetMapping("/image/{userId}")
    public void getHeaderStringByUserId(@PathVariable Integer userId, HttpServletResponse response) throws Exception
    {
        byte[] bytes = imageService.getHeaderString(userId);
        response.reset();
        response.setHeader("Content-Disposition", "attachment;filename=" + filename);
        //文件下载类型--二进制文件
        response.setContentType("application/octet-stream;charset=utf-8");
        IOUtils.write(bytes,response.getOutputStream());
    }

     */
    /*
    @GetMapping("/image/{userId}")
    public String getHeaderStringByUserId(@PathVariable Integer userId) throws Exception
    {
        return imageService.getHeaderString(userId);
    }

     */
}
